/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eupmt.lab3.controllers.impl;

import com.eupmt.lab3.controllers.*;
import com.eupmt.lab3.domain.BoGarantia;
import com.eupmt.lab3.domain.Client;
import com.eupmt.lab3.domain.Producte;
import com.eupmt.lab3.exceptions.ClientNoExisteixException;
import com.eupmt.lab3.exceptions.ProducteNoExisteixException;
import com.eupmt.lab3.persistence.ibatis.dao.BoGarantiaDaoManager;
import com.eupmt.lab3.utils.Log;
import java.util.List;

/**
 *
 * @author Grup1 Lab3
 */
public class BoGarantiaController implements IBoGarantiaController {

    //Atributs.
    protected static BoGarantiaController instance = null;
    BoGarantiaDaoManager dao = null;
    IProducteController producteController = null;
    IClientController clientController = null;
    private Log log = null;

    //Constructors.
    private BoGarantiaController() {
        log = new Log();
    }

    public void saveBoGarantia(BoGarantia boGarantia) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();
        dao.saveBoGarantia(boGarantia);
    }

    public void insertBoGarantia(BoGarantia boGarantia) throws Exception {

        dao = BoGarantiaDaoManager.getInstance();

        clientController = ClientController.getInstance();
        Client c1 = null;
        c1 = clientController.getClientById(boGarantia.getIdclient());

        producteController = ProducteController.getInstance();
        Producte p1 = null;
        p1 = producteController.getProducteById(boGarantia.getIdproducte());

        if (c1 == null) {
            throw new ClientNoExisteixException();
        } else if (p1 == null) {
            throw new ProducteNoExisteixException();

        } else {
            dao.insertBoGarantia(boGarantia);
        }


    }

    public void updateBoGarantia(BoGarantia boGarantia) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();
        dao.updateBoGarantia(boGarantia);
    }

    public void deleteBoGarantia(BoGarantia boGarantia) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();
        dao.deleteBoGarantia(boGarantia);
        
    }

    public BoGarantia getBoGarantiaById(int idBoGarantia) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();
        return dao.getBoGarantiaById(idBoGarantia);
    }

    public List<BoGarantia> getBonsGarantiaByClient(Client client) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();

        clientController = ClientController.getInstance();
        Client c1 = null;
        c1 = clientController.getClientById(client.getIdclient());

        if (c1==null){
            throw new ClientNoExisteixException();
        }


        return dao.getBonsGarantiaByClient(client);
    }

    public List<BoGarantia> getBonsGarantiaByProducte(Producte producte) throws Exception {
        dao = BoGarantiaDaoManager.getInstance();
        producteController = ProducteController.getInstance();
        Producte p1 = null;
        p1 = producteController.getProducteById(producte.getIdproducte());

        if (p1==null){
            throw new ProducteNoExisteixException();
        }
        return dao.getBonsGarantiaByProducte(producte);


    }

    // Patro Singleton
    public static BoGarantiaController getInstance() {
        if (instance == null) {
            instance = new BoGarantiaController();
        }
        return instance;
    }
}
